using System;
using Microsoft.AspNetCore.Mvc;
using Api.Demo.Data;
using Api.Demo.Models;
using Api.Demo.Entity;
using System.Linq;
using Api.Demo.Utils;
namespace Api.Demo.Controllers
{

    [ApiController]
    [Route("/api")]
    public class UserController : ControllerBase
    {
        private DbContextDemo DbDemo { get; set; }


        public UserController(DbContextDemo mydb)
        {
            DbDemo = mydb;
        }

        /// <summary>
        /// 获取所有用户
        /// </summary>
        /// <returns></returns>
        [HttpGet("getuser")]
        public ActionResult GetUsers()
        {
            var users = DbDemo.Users.ToList();

            return Ok(new
            {
                code = 200,
                msg = "ok",
                data = users
            });
        }

        /// <summary>
        /// 获取指定用户信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("getuser/{id}")]
        public ActionResult GetUser(int id)
        {

            var user = DbDemo.Users.Where(u => u.Id == id).FirstOrDefault();


            dynamic result;

            if (user != null)
            {
                result = new
                {
                    code = 200,
                    msg = "ok",
                    data = user
                };
            }
            else
            {
                result = new
                {
                    err = 140,
                    msg = $"不存在id为:${id}的用户"
                };
            }

            return new JsonResult(result);
        }



        /// <summary>
        /// 添加用户
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost("adduser")]
        public ActionResult AddUser(NewUser model)
        {
            var user = new Users
            {
                Username = model.Username,
                Password = model.Password,
                Remarks = model.Remark
            };
            DbDemo.Add(user);
            DbDemo.SaveChanges();

            return Ok(new
            {
                code = 200,
                data = user,
                msg = "ok"
            });
        }


        /// <summary>
        /// 编辑用户信息
        /// </summary>
        /// <param name="id"></param>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPatch("user/{id}")]
        public ActionResult EditUser(int id, NewUser model)
        {
            var user = DbDemo.Users.Find(id);


            dynamic result;

            if (user != null)
            {
                user.Username=model.Username;
                user.Password=model.Password;
                user.Remarks=model.Remark;

                DbDemo.SaveChanges();
                result = new
                {
                    code = 200,
                    msg = "ok",
                    data = user
                };
            }
            else
            {
                result = new
                {
                    err = 140,
                    msg = $"不存在id为:{id}的用户"
                };
            }

            return new JsonResult(result);

        }

        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete("remove/{id}")]
        public ActionResult RemoveUser(int id)
        {
            var user = DbDemo.Users.Find(id);


            dynamic result;

            if (user != null)
            {
                user.IsDeleted=1;

                DbDemo.SaveChanges();

                result = new
                {
                    code = 200,
                    msg = "ok",
                    data = user
                };
            }
            else
            {
                result = new
                {
                    err = 140,
                    msg = $"不存在id为:{id}的用户"
                };
            }

            return new JsonResult(result);
        }

    }
}